<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Carbon\Carbon;
use App\Services\ImageUrlService;

/**
 * 商品模型
 *
 * @property int $id 主键ID
 * @property string $title 商品标题
 * @property string $thumb 商品缩略图
 * @property string $description 商品描述
 * @property decimal $price 商品价格
 * @property decimal $market_price 市场价格
 * @property int $stock 库存数量
 * @property string $sku 商品SKU
 * @property int $is_new 是否新品(0否,1是)
 * @property int $is_hot 是否热销(0否,1是)
 * @property int $is_discount 是否特价(0否,1是)
 * @property int $is_recommand 是否推荐(0否,1是)
 * @property int $status 商品状态(0下架,1上架)
 * @property int $is_deleted 是否删除(0否,1是)
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 */
class Goods extends Model
{
    protected $table = 'ims_yz_goods';
    
    protected $fillable = [
        'title',
        'thumb',
        'description',
        'price',
        'market_price',
        'cost_price',
        'stock',
        'sku',
        'weight',
        'is_new',
        'is_hot',
        'is_discount',
        'is_recommand',
        'status',
        'is_deleted',
        'brand_id',
        'display_order',
        'type',
        'is_plugin',
        'plugin_id'
    ];

    protected $hidden = [];
    
    protected $casts = [
        'id' => 'integer',
        'price' => 'decimal:2',
        'market_price' => 'decimal:2',
        'stock' => 'integer',
        'is_new' => 'integer',
        'is_hot' => 'integer',
        'is_discount' => 'integer',
        'is_recommand' => 'integer',
        'status' => 'integer',
        'is_deleted' => 'integer',
    ];

    public $timestamps = true;

    public function getDateFormat()
    {
        return 'U';
    }

    public function getCreatedAtAttribute($value)
    {
        return $value ? Carbon::createFromTimestamp($value) : null;
    }

    public function getUpdatedAtAttribute($value)
    {
        return $value ? Carbon::createFromTimestamp($value) : null;
    }

    /**
     * 关联门店商品
     */
    public function storeGoods(): HasMany
    {
        return $this->hasMany(StoreGoods::class, 'goods_id', 'id');
    }

    /**
     * 关联评论
     */
    public function comments(): HasMany
    {
        return $this->hasMany(Comment::class, 'goods_id', 'id');
    }

    /**
     * 查询上架商品
     */
    public function scopeActive($query)
    {
        return $query->where('status', 1)->where('is_deleted', 0);
    }

    /**
     * 查询新品
     */
    public function scopeNew($query)
    {
        return $query->where('is_new', 1);
    }

    /**
     * 查询热销商品
     */
    public function scopeHot($query)
    {
        return $query->where('is_hot', 1);
    }

    /**
     * 查询特价商品
     */
    public function scopeDiscount($query)
    {
        return $query->where('is_discount', 1);
    }

    /**
     * 查询推荐商品
     */
    public function scopeRecommend($query)
    {
        return $query->where('is_recommand', 1);
    }

    /**
     * 根据关键词搜索
     */
    public function scopeSearch($query, $keyword)
    {
        if (!empty($keyword)) {
            return $query->where('title', 'like', "%{$keyword}%")
                ->orWhere('description', 'like', "%{$keyword}%");
        }
        return $query;
    }

    /**
     * 获取商品图片完整URL
     */
    public function getFullImageUrlAttribute()
    {
        if (empty($this->thumb)) {
            return '';
        }
        
        // 如果已经是完整URL，直接返回
        if (filter_var($this->thumb, FILTER_VALIDATE_URL)) {
            return $this->thumb;
        }
        
        // 使用统一的图片URL处理函数
        return ImageUrlService::processImageUrl($this->thumb);
    }
}
